package cv.gzd.robot.service.impl;

import cv.gzd.common.utils.Page;
import cv.gzd.robot.mapper.UserDao;
import cv.gzd.robot.pojo.User;
import cv.gzd.robot.pojo.vo.UserQueryVo;
import cv.gzd.robot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * @Author: Jun
 * @Date: 2018/8/15 23:09
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public Page<User> selectPageByUserQueryVo(UserQueryVo userVo) {
        Page<User> page=new Page<>();
        page.setSize(5);
        if(userVo!=null){
            userVo.setSize(5);
            if(userVo.getPage()!=null){
                page.setPage(userVo.getPage());
                userVo.setStartRow((userVo.getPage()-1)*userVo.getSize());
            }
            if(null!=userVo.getUsername()&&!"".equals(userVo.getUsername().trim())){
                userVo.setUsername(userVo.getUsername().trim());
            }
            if(null!=userVo.getAuthorization()&&!"".equals(userVo.getAuthorization().trim())){
                userVo.setAuthorization(userVo.getAuthorization().trim());
            }
            if(null!=userVo.getRealname()&&!"".equals(userVo.getRealname().trim())){
                userVo.setRealname(userVo.getRealname().trim());
            }
            page.setTotal(userDao.selectUserCountByUserQueryVo(userVo));
            page.setRows(userDao.selectUserListUserQueryVo(userVo));
        }
        return page;
    }

    @Override
    public User selectUserByUid(Integer id) {
        return userDao.selectUserByUid(id);
    }

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userDao.deleteUser(id);
    }

    @Override
    public void saveUser(User user) {
        userDao.saveUser(user);
    }

    @Override
    public User selectUserByUserName(String username) {
        return userDao.selectUserByUserName(username);
    }

    @Override
    public User selectUserByUserNameAndPassword(String username, String password) {
        return userDao.selectUserByUserNameAndPassword(username,password);
    }

    @Override
    public void initRoot() {
        int rows=userDao.selectByRoot();
        if (rows==0){
            User user=new User();
            user.setUsername("root");
            user.setPassword("root");
            user.setAuthorization("超级管理员");
            user.setRealname("boss");
            user.setPhone("1111");
            userDao.saveUser(user);
        }
    }
}
